/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package TextManager;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import naivebayes.DocumentsGroup;

/**
 *
 * @author Jaime
 */
public class FilesReader {
    
    private final DocumentsGroup group;
    
    public FilesReader(DocumentsGroup group)
    {
        this.group = group;
        
    }
    
    public void read(File source, boolean positive) throws FileNotFoundException, IOException
    {
        group.addFile(source, positive);
        
        FileReader fr = new FileReader(source);
        BufferedReader br = new BufferedReader(fr);
        
        read(br, positive);
        
        fr.close();
        br.close();
        
        
    }
    
    /* private void read(BufferedReader source, boolean positive) throws IOException
    {
        String line;
        
        while((line = source.readLine()) != null)
        {
            if(line.equals(""))
              continue;
           
            line = WordCleaner.clean(line);
            
            StringTokenizer st = new StringTokenizer(line, " ");
            
            while(st.hasMoreElements())
            {
                WordOccurrence word = new WordOccurrence(st.nextToken());
                if(shouldIncludeWord(word))
                {
                    if(positive)
                    {
                        group.addPositiveWord(word);
                    }
                    else
                    {
                        group.addNegativeWord(word);
                    }
                }
            }
            
        }
    } */

    
    private void read(BufferedReader source, boolean positive) throws IOException
    {
        FileWordStream fws = new FileWordStream(source);        
               
        while(fws.hasNextWord())
        {

                WordOccurrence word = fws.nextWord();
                if(shouldIncludeWord(word))
                {
                    if(positive)
                    {
                        group.addPositiveWord(word);
                    }
                    else
                    {
                        group.addNegativeWord(word);
                    }
                }
            
            
        }
    }
    
    private boolean shouldIncludeWord(WordOccurrence word) {
        return !(word.isEmpty());
    }
    
    public String toString()
    {
        return group.toString();
    }
            
    
}
